﻿using CcrFlows.Core;
using Microsoft.Ccr.Core;

namespace CcrFlows.Internal.Stages
{
    class ForkStage<TIn, TOut0, TOut1> : StageBase
    {
        internal ForkStage(ForkHandler<TIn, TOut0, TOut1> handler)
            : base(new[] { new Port<TIn>() }, new IPort[] { new Port<TOut0>(), new Port<TOut1>() })
        {
            base.InitializeInputHandler(Arbiter.Receive(
                                        false,
                                        (Port<TIn>)base.inputPorts[0],
                                        i =>
                                        {
                                            handler(i, (Port<TOut0>)base.outputPorts[0], (Port<TOut1>)base.outputPorts[1]);
                                            base.WaitForNextInput();
                                        }
                                        ));
        }
    }
}
